<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/背景图/2.png" style="width: 50px;"/>
        <span>新京淘电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout()">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px' ">

        <div class="leftCollapse" @click="">|||</div>

        <el-menu background-color="#2C3E50" text-color="#fff" active-text-color="#4094ff" unique-opened
        :collapse="isCollapse" :collapse-transition="isCollapseTransition" router  :default-active="defaultActive">

        <!-- 下方定义一级菜单 -->
        <el-submenu :index="menu.id+''" v-for="menu in menuList" :key="menu.id">
          <template slot="title">
            <i class="menuIcon[menu.id]"></i>
            <span >{{menu.name}}</span>
          </template>
             <!-- 定义二级菜单 -->
             <el-menu-item :index="childrenMenu.path" v-for="childrenMenu in menu.children" :key="childrenMenu.id"
             @click="defaultActiveMenu(childrenMenu.path)">
               <template slot="title">
                 <i class="el-icon-menu"></i>
                 <span>{{childrenMenu.name}}</span>
               </template>
             </el-menu-item>
        </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    this.getMenuList()
    this.defaultActive = window.sessionStorage.getItem('activeMenu')
  },
  data () {
    return {
      isCollapse: false,
      isCollapseTransition: false,
      defaultActive: '',
      menuList: [],
      menuIcon: {
        1: 'iconfont iconuser',
        3: 'iconfont iconshangpin',
        5: 'iconfont iconicon--copy',
        7: 'iconfont iconquanxian',
        8: 'iconfont iconziyuan'
      }
    }
  },
  methods: {
    // 删除session中的数据，再调用登录页面即可退出登录
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 设定左侧折叠展开的效果
    collspseClick () {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList () {
      const { data: result } = await this.$http.get('/rights/getRightsList')
      if (result.status !== 200) return this.$message.error('左侧菜单查询失败')
      this.menuList = result.data
    },
    defaultActiveMenu (activeMenu) {
      window.sessionStorage.setItem('activeMenu', activeMenu)
      this.defaultActive = activeMenu
    }
  }

}
</script>
<style lang="less" scoped>
.el-header{
  background-color: blueviolet;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color:papayawhip;
  padding-left: 1px;
}
div{
  display: flex;
  align-items: center;
}
span{
  margin-left: 15px;
  font-size: 25px;
}
.el-aside{

  background-color: blueviolet;

  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: beige;
}
.home-container{
  height: 100%;
  width: 100%;
}
.iconfont{
  margin-right: 8px;
}
.leftCollapse{
  background-color: #708090;
  font-size: 10px;
  line-height: 30px;
  color: #FFFFFF;
  text-align:center;
  letter-spacing: 4px;
  cursor: pointer;
}
</style>
